﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YJKStairs.AddSigns
{
	internal abstract class PackerBase
	{
		internal int UseI
		{
			get
			{
				return this.UseIndex;
			}
		}

		internal Transform UseTransform
		{
			get
			{
				return this.useTransform;
			}
		}

		internal List<double> AddParamValue
		{
			get
			{
				return this.ListAddParamValues;
			}
		}

		internal Dictionary<string, Parameter> DicParameters
		{
			get
			{
				return this.dicParameters;
			}
		}

		internal ArchConditonEnum ArchCondition
		{
			get
			{
				return this.archCondition;
			}
		}

		internal LocationPoint ThisFamilyInstanceLocation
		{
			get
			{
				return this.ThisFamilyInstance.Location as LocationPoint;
			}
		}

		internal ElementId ThisFamilyInstanceLevelId
		{
			get
			{
				return this.ThisFamilyInstance.LevelId;
			}
		}

		internal bool SetFamilyInstance(FamilyInstance input, Transform useTransform = null)
		{
			if (!this.ifCanUse(input))
			{
				return false;
			}
			this.ThisFamilyInstance = input;
			this.makeDic();
			this.makeArchCondtion();
			StaticRescours.TempFamilyName = StaticRescours.TempFamilyNames[this.UseIndex];
			this.useTransform = useTransform;
			return true;
		}

		private bool ifCanUse(FamilyInstance input)
		{
            return input.Category.Id.IntegerValue == (int)StaticRescours.StairCategories[this.UseIndex] && Regex.IsMatch(input.Symbol.FamilyName, StaticRescours.FamilyNames[this.UseIndex]) && this.IfCanUseAddMore(input);
		}

		protected virtual bool IfCanUseAddMore(FamilyInstance input)
		{
			return true;
		}

		private void makeArchCondtion()
		{
			string text = this.ThisFamilyInstance.GetParameter(StaticRescours.ArchCoditionParamNameOfStair[this.UseIndex]).AsString();
			if (text.Equals(StaticRescours.StrUpUseInParam[this.UseIndex]))
			{
				this.archCondition = ArchConditonEnum.UP;
			}
			else if (text.Equals(StaticRescours.StrDownUseInParam[this.UseIndex]))
			{
				this.archCondition = ArchConditonEnum.DOWN;
			}
			else
			{
				this.archCondition = ArchConditonEnum.NONE;
			}
			bool flag = false;
			bool flag2 = false;
			switch (this.archCondition)
			{
			case ArchConditonEnum.UP:
				foreach (string paramName in StaticRescours.ArchDrictionParamNameUp[this.UseI])
				{
					Parameter parameter = this.ThisFamilyInstance.GetParameter(paramName);
					if (parameter.AsInteger() != 0)
					{
						if (flag2)
						{
							flag = false;
							break;
						}
						flag = true;
						flag2 = true;
					}
				}
				break;
			case ArchConditonEnum.DOWN:
				foreach (string paramName2 in StaticRescours.ArchDrictionParamNameDown[this.UseI])
				{
					Parameter parameter = this.ThisFamilyInstance.GetParameter(paramName2);
					if (parameter.AsInteger() != 0)
					{
						if (flag2)
						{
							flag = false;
							break;
						}
						flag = true;
						flag2 = true;
					}
				}
				break;
			}
			if (!flag)
			{
				this.archCondition = ArchConditonEnum.NONE;
			}
			this.MakeArchConditonMore();
		}

		protected virtual void MakeArchConditonMore()
		{
		}

		private void makeDic()
		{
			this.dicParameters.Clear();
			foreach (string text in StaticRescours.ParamNamesStairs[this.UseIndex])
			{
				Parameter parameter = this.ThisFamilyInstance.GetParameter(text);
				this.dicParameters.Add(text, parameter);
			}
			this.MakeDicAddMore();
		}

		protected virtual void MakeDicAddMore()
		{
		}

		private Transform useTransform;

		protected FamilyInstance ThisFamilyInstance;

		private ArchConditonEnum archCondition = ArchConditonEnum.NONE;

		protected int UseIndex = -1;

		protected List<double> ListAddParamValues = new List<double>();

		private Dictionary<string, Parameter> dicParameters = new Dictionary<string, Parameter>();

		internal string ThisLevelName;
	}
}
